package crmdna.registration;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;
import crmdna.registration.Registration.RegistrationStatus;

@Api(name = "registration", description = "Registration (Alpha)")
public class RegistrationApi {
	@ApiMethod(path = "query", httpMethod = HttpMethod.GET)
	public APIResponse query(@Nullable @Named("client") String client,
			@Nullable @Named("programId") Long programId,
			@Nullable @Named("searchStr") String searchStr,
			@Nullable @Named("status") RegistrationStatus status,
			@Nullable @Named("sortByFirstName") Boolean sortByFirstName,
			User user) {

		try {
			if (null == client)
				client = "isha";

			RegistrationQueryCondition qc = new RegistrationQueryCondition();
			qc.programId = programId;
			qc.searchStr = searchStr;
			qc.status = status;

			if (sortByFirstName == null)
				sortByFirstName = false;
			qc.sortByFirstName = sortByFirstName;

			List<RegistrationProp> props = Registration.query(client, qc,
					Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, props);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "queryByTransactionId", httpMethod = HttpMethod.GET)
	public APIResponse queryByTransactionId(
			@Nullable @Named("client") String client,
			@Named("transactionId") String transactionId) {

		try {
			if (null == client)
				client = "isha";

			RegistrationProp registrationProp = Registration
					.queryByTransactionId(client, transactionId);

			return new APIResponse(Status.SUCCESS, registrationProp);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "getByTransactionId", httpMethod = HttpMethod.GET)
	public APIResponse getByTransactionId(
			@Nullable @Named("client") String client,
			@Named("transactionId") String transactionId) {

		try {
			if (null == client)
				client = "isha";

			RegistrationProp registrationProp = Registration
					.getByTransactionId(client, transactionId);

			return new APIResponse(Status.SUCCESS, registrationProp);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "queryRegistrationStatus", httpMethod = HttpMethod.GET)
	public APIResponse queryRegistrationStatus(
			@Nullable @Named("client") String client,
			@Named("programId") long programId, @Named("email") String email,
			@Named("firstName") String firstName,
			@Nullable @Named("lastName") String lastName) {

		try {
			if (null == client)
				client = "isha";

			RegistrationStatusProp registrationStatusProp = Registration
					.queryRegistrationStatus(client, programId, email,
							firstName, lastName);

			return new APIResponse(Status.SUCCESS, registrationStatusProp);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "getSummary", httpMethod = HttpMethod.GET)
	public APIResponse getSummary(@Nullable @Named("client") String client,
			@Named("programId") long programId, User user) {

		try {
			if (null == client)
				client = "isha";

			RegistrationSummaryProp registrationSummaryProp = Registration
					.getSummary(client, programId, Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, registrationSummaryProp);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
}
